package com.hj.service.impl;

import com.hj.dao.CompanyMapper;
import com.hj.dao.CustomerStopMapper;
import com.hj.dao.StopMapper;
import com.hj.entity.Company;
import com.hj.entity.Ss_accesscode;
import com.hj.service.CustomerStopService;
import com.hj.utils.MultipleDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.util.List;

@Service("customerStopService")
public class CustomerStopServiceImpl implements CustomerStopService {

    private static Logger logger = LoggerFactory.getLogger(CustomerStopServiceImpl.class);

    @Autowired
    private CustomerStopMapper customerStopMapper;

    @Autowired
    private CompanyMapper companymapper;

    @Autowired
    private StopMapper stopmapper;

    @Value("${cp.agency.need.closedown}")
    private String agencyNames;

    @Override
    @Scheduled(cron="0 0/4 * * * ?")
    public void customerStopJob() {

        String[] agencys = agencyNames.split(",");

        MultipleDataSource.setDataSourceKey("dataSource2");
        List<Company> companyList = customerStopMapper.search_cp(agencys);

        //查询所有需停机的400号码接入策略并插入备份表
        MultipleDataSource.setDataSourceKey("dataSource");
        List<Ss_accesscode> accesscodeList = stopmapper.search_needStop(companyList);

        if (accesscodeList.size()>0) {

            MultipleDataSource.setDataSourceKey("dataSource2");
            //修改400号码为停机状态
            companymapper.update_companyStop(companyList);
            //将停机前的接入策略插入到备份表
            stopmapper.insert_strategyRecord(accesscodeList);

            //逐个停机
            MultipleDataSource.setDataSourceKey("dataSource");
            String vccId="";
            for (Ss_accesscode accesscode : accesscodeList) {
                vccId += accesscode.getVccId()+" ";
                stopmapper.update_stop(accesscode.getVccId());
            }

            logger.info("定制代理商按余额停机列表： {}", vccId);
        }

    }
}
